<?php

class MUser extends MY_Model {

    protected $table = 'user';

    public static function getAuthState($state=NULL){
        $define = array(
            '0'=>'未认证',
            '1'=>'待审核',
            '2'=>'未通过',
            '3'=>'已认证'
        );
        if($state === NULL){
            return $define;
        }else{
            return key_exists($state, $define)?$define[$state]:'';
        }
    }

    public static function getPromiseState($state=NULL){
        $define = array(
            '0'=>'未缴纳',
            '1'=>'已缴纳',
            '3'=>'已退还'
        );
        if($state === NULL){
            return $define;
        }else{
            return key_exists($state, $define)?$define[$state]:'';
        }
    }

    public static function getSex($sex=NULL){
        $define = array(
            '0'=>'未知',
            '1'=>'男',
            '2'=>'女'
        );
        if($sex === NULL){
            return $define;
        }else{
            return key_exists($sex, $define)?$define[$sex]:'';
        }
    }

    public static function getUserType($type=NULL){
        $define = array(
            '0'=>'已拉黑',
            '1'=>'正常用户'
        );
        if($type === NULL){
            return $define;
        }else{
            return key_exists($type, $define)?$define[$type]:'';
        }
    }

    public static function getWxBind($type=NULL){
        $define = array(
            '0'=>'未绑定',
            '1'=>'已绑定'
        );
        if($type === NULL){
            return $define;
        }else{
            return key_exists($type, $define)?$define[$type]:'';
        }
    }

    public function getList($limit){
        $this->db->select( '*');
        $this->db->distinct();
        $this->db->from($this->table);
        $this->db->limit($limit['limit'],$limit['offset']);
        $this->formfilter->doFilter();
        $this->db->order_by('id', 'desc');
        $query = $this->db->get();
        return $query->result();
    }

    public function getCount(){
        $this->db->select('count(distinct id) as num');
        $this->db->from($this->table);
        $this->formfilter->doFilter();
        $tmp = $this->db->get()->result();
        return (int)$tmp[0]->num;
    }

    public function getById($id){
        $query = $this->db->where('id',$id)->get($this->table);
        $result = $query->row();
        return $result;
    }

    public function getAllUser($where=null,$order_by=null){
        $arr = array();
        $this->db->select("*");
        $this->db->from($this->table);
        $result = $this->db->get()->result();
        foreach ($result as $v){
            $arr[$v->id] = $v->nick_name;
        }
        return $arr;
    }

    public function getTotal(){
        $yesterday = IOSS_StaticConfig::get_yesterday();
        $week = IOSS_StaticConfig::get_b_and_e();
        $month = IOSS_StaticConfig::get_this_month();
        $total = $this->db->select('count(id) as num')
            ->from($this->table)
            ->get()
            ->row();
        $yesterday_num = $this->db->select('count(id) as num')
            ->from($this->table)
            ->where('create_time >= '.$yesterday['start'])
            ->where('create_time <= '.$yesterday['end'])
            ->get()
            ->row();
        $week_num = $this->db->select('count(id) as num')
            ->from($this->table)
            ->where('create_time >= '.$week[0])
            ->where('create_time <= '.$week[1])
            ->get()
            ->row();
        $month_num = $this->db->select('count(id) as num')
            ->from($this->table)
            ->where('create_time >= '.$month['start_month'])
            ->where('create_time <= '.$month['end_month'])
            ->get()
            ->row();
        return (object)[
            'yesterday' => $yesterday_num->num,
            'week' => $week_num->num,
            'month' => $month_num->num,
            'total' => $total->num
        ];
    }
}
